<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    // protected $table="profits";
    protected $step_config = ['第一步', '第二步', '第三步', '第四步', '第五步', '第六步', '第七步', '第八步', '第九步', '第十步'];

    //
    public function tasktype()
    {
        return $this->hasOne(TaskType::class, 'id', 'type_id');
    }

    public function get_sort_type($request)
    {
        $sort_type = '';
        switch ($request->sort_type) {
            case '默认排序':
                $sort_type = 'id';
                break;
            case '奖励最高':
                $sort_type = 'money';
                break;
            case '结算最快':
                $sort_type = 'dead_line';
                break;
            case '最新发布':
                $sort_type = 'created_at';
                break;
            default:
                $sort_type = 'id';
                break;
        }

        return $sort_type;
    }

    //获取任务列表
    public function get_list($request)
    {
        $sort_type = $this->get_sort_type($request);
        $task_type_where = [];
        if (!empty($request->task_type)) {
            $task_type_where = [['name', '=', $request->task_type]];
        }
        $data = $this->whereHas('task_type', function ($query) use ($task_type_where) {
            $query->where($task_type_where);
        })->orderBy($sort_type, 'desc')->paginate(10);
        $data = $data->map(function ($item, $key) {
            $new_item = [];
            $new_item['task_id'] = $item->id;
            $new_item['name'] = $item->name;
            $new_item['e_time'] = date('Y-m-d', strtotime($item->e_time));
            $new_item['total_count'] = $item->total_count;
            $new_item['money'] = $item->money;
            $new_item['surplus'] = $item->surplus;

            return $new_item;
        });

        return $data;
    }

    public function task_type()
    {
        return $this->hasOne(TaskType::class, 'id', 'type_id');
    }

    public function task_step()
    {
        return $this->hasMany(TaskStep::class, 'task_id', 'id')->orderBy('step', 'asc');
    }

    public function task_submit()
    {
        return $this->hasMany(TaskSubmit::class,'task_id','id');
    }
    //获取任务详情
    public function get_info($request)
    {
        $data = $this->where('id', $request->task_id)->with('task_step')->first();

        $info = [];
        $info['id'] = $data->id;
        $info['name'] = $data->name;
        $info['surplus'] = $data->surplus;
        $info['total_count'] = $data->total_count;
        $info['frequency'] = $data->frequency;
        $info['money'] = $data->money;
        $info['s_time'] = date('Y-m-d', strtotime($data->s_time));
        $info['e_time'] = date('Y-m-d', strtotime($data->e_time));

        $info['spend_time'] = $data->spend_time;
        $info['submit_num'] = $data->submit_num;
        $info['dead_line'] = $data->dead_line;
        $info['task_detail'] = $data->task_detail;

        $info['task_step'] = $data->task_step->map(function ($item, $key) {
            $data = [];
            $data['title'] = $this->step_config[$item->step - 1];
            $data['step_detail'] = $item->step_detail;
            $data['step_img'] = $item->step_img;

            return $data;
        });

        //任务是否领取
        $order = Order::where('user_id', $request->user->id)->where('task_id', $request->task_id)->where('status', 0)->first();
        if ($order) {
            $info['task_state'] = 1;
        } else {
            $info['task_state'] = 0;
        }
        $info['e_date_time'] = $data->e_time;

        return $info;
    }
    
}
